package xyz.xtt.equity.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import xyz.xtt.equity.entity.TblBenefitPurchaseRecord;
import xyz.xtt.equity.mapper.TblBenefitPurchaseRecordMapper;
import xyz.xtt.equity.service.ITblBenefitPurchaseRecordService;

/**
 * <p>
 * 权益售卖记录表 服务实现类
 * </p>
 *
 * @author dale
 * @since 2025-02-10
 */
@Service
public class TblBenefitPurchaseRecordServiceImpl extends ServiceImpl<TblBenefitPurchaseRecordMapper, TblBenefitPurchaseRecord>
		implements ITblBenefitPurchaseRecordService {

	@Override
	public List<TblBenefitPurchaseRecord> getPurchaseRecord(long appUserId, Long packageId) {
		LambdaQueryChainWrapper<TblBenefitPurchaseRecord> queryChainWrapper = new LambdaQueryChainWrapper<>(baseMapper);
		return queryChainWrapper.eq(TblBenefitPurchaseRecord::getAppUserId, appUserId).eq(TblBenefitPurchaseRecord::getPackageId, packageId).list();
	}

	@Override
	public long countByBusinessOrderId(Long businessOrderId) {
		return this.lambdaQuery().eq(TblBenefitPurchaseRecord::getBusinessOrderId, businessOrderId).count();
	}

	@Override
	public List<TblBenefitPurchaseRecord> getAssetNoPurchaseRecords(List<Long> assetNodIds, Long userId) {
		if (assetNodIds != null && !assetNodIds.isEmpty()) {
			return this.lambdaQuery().in(TblBenefitPurchaseRecord::getAssetNoId, assetNodIds).eq(TblBenefitPurchaseRecord::getAppUserId, userId).list();
		}
		return List.of();
	}

	@Override
	public boolean blockBenefitStatus(List<Long> assetNodIds, Long userId) {
		return this
				.lambdaUpdate()
				.in(TblBenefitPurchaseRecord::getAssetNoId, assetNodIds)
				.eq(TblBenefitPurchaseRecord::getAppUserId, userId)
				.set(TblBenefitPurchaseRecord::getBenefitStatus, 1)
				.set(TblBenefitPurchaseRecord::getUpdateTime, LocalDateTime.now())
				.update();
	}

	@Override
	public boolean unblockBenefitStatus(List<Long> assetNodIds, Long userId) {
		return this
				.lambdaUpdate()
				.in(TblBenefitPurchaseRecord::getAssetNoId, assetNodIds)
				.eq(TblBenefitPurchaseRecord::getAppUserId, userId)
				.set(TblBenefitPurchaseRecord::getBenefitStatus, 0)
				.set(TblBenefitPurchaseRecord::getUpdateTime, LocalDateTime.now())
				.update();
	}

	@Override
	public List<TblBenefitPurchaseRecord> getAssetNoPurchaseRecordsNotBlocked(List<Long> assetNodIds, Long receiverId) {
		if (assetNodIds != null && !assetNodIds.isEmpty()) {
			return this
					.lambdaQuery()
					.in(TblBenefitPurchaseRecord::getAssetNoId, assetNodIds)
					.eq(TblBenefitPurchaseRecord::getAppUserId, receiverId)
					.eq(TblBenefitPurchaseRecord::getBenefitStatus, 0)
					.list();
		}
		return List.of();
	}
}
